package top.xzxsrq.fileviewutils.download;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.stereotype.Component;
import top.xzxsrq.utils.PathUtils;
import top.xzxsrq.web.download.SimilarFeiJiSuD;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@EqualsAndHashCode(callSuper = true)
@Component
@Data
public class SimilarFeiJiSuDownload extends SimilarFeiJiSuD {
    private String saveDir = "saveDir"; // 文件保存位置
    private String videoStart = "videoStart"; // 视频开始位置
    private String videoEnd = "videoEnd"; // 视频结束位置
    private String nameLength = "nameLength"; // 名字的长度
    private String templatePageUrl = "templatePageUrl"; // 下载模板url http://m.feijisu36.com/zongyi/90182/${}.html

    @Override
    public void webRunFunc(String downloadTaskId, String userBasePath, JSONObject jsonObject) throws Exception {
        String saveDir = jsonObject.getString(this.saveDir);
        saveDir = PathUtils.pathResolve(userBasePath, saveDir);
        SimilarFeiJiSuDownload feiJiSuD = new SimilarFeiJiSuDownload();
        feiJiSuD.downloadTaskId = downloadTaskId;
        feiJiSuD.downloadPages(
                saveDir,
                jsonObject.getInteger(videoStart),
                jsonObject.getInteger(videoEnd),
                jsonObject.getInteger(nameLength),
                jsonObject.getString(templatePageUrl)
        );
    }

    @Override
    public List<Map<Object, Object>> webRunFuncParams() {
        List<Map<Object, Object>> result = new LinkedList<>();
        result.add(webRunFuncParam(
                "prop",
                "",
                "label",
                "下载说明",
                "type",
                "downloadInstructions",
                "value",
                "这是类似飞极速下载\n" +
                        "下载模板url http://m.feijisu36.com/zongyi/90182/${}.html\n" +
                        "${}表示替换的是视频开始位置到结束位置"
        ));
        result.add(webRunFuncParam(
                "prop",
                saveDir,
                "label",
                "保存到文件夹",
                "type",
                "SelectBasePath"
        ));
        result.add(webRunFuncParam(
                "prop",
                videoStart,
                "label",
                "视频开始",
                "type",
                "inputNumber"
        ));
        result.add(webRunFuncParam(
                "prop",
                videoEnd,
                "label",
                "视频结束",
                "type",
                "inputNumber"
        ));
        result.add(webRunFuncParam(
                "prop",
                nameLength,
                "label",
                "名字长度",
                "type",
                "inputNumber"
        ));
        result.add(webRunFuncParam(
                "prop",
                templatePageUrl,
                "label",
                "模板url",
                "type",
                "input",
                "props",
                webRunFuncParam(
                        "type",
                        "textarea",
                        "autosize",
                        "true"
                )
        ));
        return result;
    }
}
